import requests;
from _ZSGK import zsgk_storage;

'''
ZSGK spider
'''
__author__ = 'fuhaotian';

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0"
};

def get_universities(start, end):

    base_url_pattern = "https://api.eol.cn/gkcx/api/?access_token=&admissions=&central=&department=" \
                       "&dual_class=&f211=&f985=&is_doublehigh=&is_dual_class=&keyword=&nature=&page=%d" \
                       "&province_id=&request_type=1&school_type=&signsafe=&size=15&sort=view_total" \
                       "&top_school_id=[770,771,3186]&type=&uri=apidata/api/gk/school/lists";
    url_list = list(base_url_pattern % item for item in range(start, end + 1));
    universities = [];

    for url in url_list:
        print("Page: %d, Visit url: %s" % (url_list.index(url) + start, url));
        r = requests.get(url, headers=headers);
        if r.status_code == 200:
            current_universities = r.json().get("data").get("item");
            print(current_universities);
            zsgk_storage.universities_mysql(universities);
            universities += current_universities;
        else:
            print("Request error, return status code is: %d" % (r.status_code,));

    return universities;

if __name__ == "__main__":
    universities = get_universities(134, 190);